package com.tg.app.activity.device.ui.cameraview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.tg.app.R;
import com.tange.base.toolkit.TGThreadPool;

public class LostFilePopupWindowHelper {
    private PopupWindow mLostFilePopupWindow;
    public void show(Context context, @StringRes int resid){
        TGThreadPool.executeOnUiThread(() -> {
            @SuppressLint("InflateParams") View contentView = LayoutInflater.from(context).inflate(R.layout.layout_msgtoast, null);
            if (mLostFilePopupWindow == null) {
                mLostFilePopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mLostFilePopupWindow.setTouchable(true);
                mLostFilePopupWindow.setOutsideTouchable(true);
                mLostFilePopupWindow.setFocusable(false);
                TextView msgtv = contentView.findViewById(R.id.msgtv);
                msgtv.setText(resid);
            }
            if (context instanceof Activity) {
                Activity activity = (Activity) context;

                if (activity.isDestroyed() || activity.isFinishing()) {
                    return;
                }

                if (mLostFilePopupWindow != null && !mLostFilePopupWindow.isShowing()) {
                    mLostFilePopupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 50);
                }
            }

        });
    }

    public void hide(){
        TGThreadPool.executeOnUiThread(() -> {
            if (mLostFilePopupWindow != null && mLostFilePopupWindow.isShowing()) {
                mLostFilePopupWindow.dismiss();
            }
        });
    }
}
